﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BiffenGUI.BiffenService;
using System.Windows.Forms;

namespace BiffenGUI
{
    class PlanShowsTableHelper
    {

        private DataGridView table;   
        private BiffenServiceClient service = null;
        private List<Show> shows = new List<Show>();

        public PlanShowsTableHelper(DataGridView table)
        {
            this.table = table;
            service = new BiffenServiceClient();

            PopulateDataGridView();
        }

        // Creates the columns and loads the data.
        private void PopulateDataGridView()
        {
            // Set the column header names.
            table.ColumnCount = 7;
            table.Columns[0].Name = "Mandag";
            table.Columns[1].Name = "Tirsdag";
            table.Columns[2].Name = "Onsdag";
            table.Columns[3].Name = "Torsdag";
            table.Columns[4].Name = "Fredag";
            table.Columns[5].Name = "Lørdag";
            table.Columns[6].Name = "Søndag";

            // Populate the rows.
            object[] row1 = new object[]{"15:00", "15:00", 
            "15:00", "15:00", "15:00", "15:00", "15:00"};
            object[] row2 = new object[]{"16:00", "16:00", 
            "16:00", "16:00", "16:00", "16:00", "16:00"};
            object[] row3 = new object[]{"17:00", "17:00", 
            "17:00", "17:00", "17:00", "17:00", "17:00"};
            object[] row4 = new object[]{"18:00", "18:00", 
            "18:00", "18:00", "18:00", "18:00", "18:00"};
            object[] row5 = new object[]{"19:00", "19:00", 
            "19:00", "19:00", "19:00", "19:00", "19:00"};
            object[] row6 = new object[]{"20:00", "20:00", 
            "20:00", "20:00", "20:00", "20:00", "20:00"};
            object[] row7 = new object[]{"21:00", "21:00", 
            "21:00", "21:00", "21:00", "21:00", "21:00"};
            object[] row8 = new object[]{"22:00", "22:00", 
            "22:00", "22:00", "22:00", "22:00", "22:00"};
            object[] row9 = new object[]{"23:00", "23:00", 
            "23:00", "23:00", "23:00", "23:00", "23:00"};
            object[] row10 = new object[]{"24:00", "24:00", 
            "24:00", "24:00", "24:00", "24:00", "24:00"};
            object[] row11 = new object[]{"01:00", "01:00", 
            "01:00", "01:00", "01:00", "01:00", "01:00"};

            // Add the rows to the DataGridView.
            object[] rows = new object[] { row1, row2, row3, row4, row5, row6, row7, row8, row9, row10, row11 };
            foreach (object[] rowArray in rows)
            {
                table.Rows.Add(rowArray);
            }

            //table.Rows[0].HeaderCell.Value = "15:00";
            //table.Rows[1].HeaderCell.Value = "16:00";
            //table.Rows[2].HeaderCell.Value = "17:00";
            //table.Rows[3].HeaderCell.Value = "18:00";
            //table.Rows[4].HeaderCell.Value = "19:00";

            // Adjust the row heights so that all content is visible.
            table.AutoResizeRows(
                DataGridViewAutoSizeRowsMode.AllCells);
        }

        public string GetRows()
        {
            string value = (table.CurrentCell.RowIndex).ToString();

            return value;
        }

        public int GetDayInWeek()
        {
            return table.CurrentCell.ColumnIndex;
        }  

        private void ResetColors()
        {

            for (int row = 0; row < table.Rows.Count; row++)
            {
                for (int col = 0; col < table.Columns.Count; col++)
                {
                    table[col, row].Style.BackColor = table.DefaultCellStyle.BackColor;
                }
            }

        }

        public void SetTable(List<Show> shows)
        {
            ResetColors();
            SetStringsOfCells();

            this.shows = shows;
            
            int row = 0;
            int col = 0;
            foreach (Show s in shows)
            {
                string dayOfWeek = s.StartTime.DayOfWeek.ToString();
                int start = s.StartTime.Hour;
                int duration = s.Duration/60;
                string title = s.Movies.ElementAt(0).Title;

                row = ChooseHour(start);
                col = ChooseDayOfWeek(dayOfWeek);

                SetTableColor(row, col, duration, title);
                
            }  
        }

        private int ChooseHour(int start)
        {
            int row = 15;

            if (start == 00)
            {
                row = 9;
            }
            else
            {
                row = start - row;
            }

            return row;    
        }

        private int ChooseDayOfWeek(string dayOfWeek)
        {
            int col = 0;
            switch (dayOfWeek)
            {

                case "Monday":
                    col = 0;
                    break;
                case "Tuesday":
                    col = 1;
                    break;
                case "Wednesday":
                    col = 2;
                    break;
                case "Thursday":
                    col = 3;
                    break;
                case "Friday":
                    col = 4;
                    break;
                case "Saturday":
                    col = 5;
                    break;
                case "Sunday":
                    col = 6;
                    break;
            }
            return col;
        }

        private void SetTableColor(int row, int col, int duration, string title)
        {
            int i = row;
            int j = 0;

            while (j < duration)
            { 
                table.Rows[i].Cells[col].Style.BackColor = System.Drawing.Color.Red;
                table.Rows[i].Cells[col].Value = title;
                i++;
                j++;
            }
        }

        public void SetStringsOfCells()
        {
            for (int col = 0; col < table.Columns.Count; col++)
            {
                table[col, 0].Value = "15:00";
                table[col, 1].Value = "16:00";
                table[col, 2].Value = "17:00";
                table[col, 3].Value = "18:00";
                table[col, 4].Value = "19:00";
                table[col, 5].Value = "20:00";
                table[col, 6].Value = "21:00";
                table[col, 7].Value = "22:00";
                table[col, 8].Value = "23:00";
                table[col, 9].Value = "24:00";
                table[col, 10].Value = "01:00";
            }
        }

        public Show GetSelectedShow(DateTime monday)
        {
            int row = table.SelectedCells[0].RowIndex;
            int col = table.SelectedCells[0].ColumnIndex;

            DateTime time = monday.AddDays(col);
            time = time.AddHours(row + 15);

            return FindShow(time);
        }

        public Show FindShow(DateTime time)
        { 
            bool found = false;
            int i = 0;
            Show show = null;

            while(i < shows.Count && !found)
            {
                if (shows[i].StartTime.Equals(time))
                {
                    found = true;
                    show = shows[i];
                }
                else
                {
                    i++;
                }          
            }
            return show;      
        }

    }
}
